import { useAppStore } from '@/stores/app'
import { createRouter, createWebHistory } from 'vue-router'
import licenseRoutes from './modules/license'
import nodeRoutes from './modules/node'
import resolveRoutes from './modules/resolve'
import siteRoutes from './modules/site'
import systemRoutes from './modules/system'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/sign-in',
      name: 'SignIn',
      component: () => import('@/views/auth/signIn.vue')
    },
    {
      path: '/install',
      name: 'InitConf',
      component: () => import('@/views/auth/InitConfig.vue')
    },
    {
      path: '/',
      name: 'workbench',
      component: () => import('@/views/layout/DefaultLayout.vue'),
      redirect: '/overview',
      children: [
        {
          path: '/overview',
          name: 'Overview',
          component: () => import('@/views/overview/Overview.vue'),
          meta: {
            title: '首页概览',
            showPageHeader: false,
            showBack: false,
            currentActiveMenu: 'Overview'
          }
        },
        ...siteRoutes,
        ...licenseRoutes,
        ...resolveRoutes,
        ...nodeRoutes,
        ...systemRoutes
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('@/views/NotFound.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  const appStore = useAppStore()
  if (to.name !== 'FlowAnalyze' && to.name !== 'safeAnalyze') {
    appStore.initSiteStatistic()
  }
  if (
    to.name !== 'NodeBandwidth' &&
    to.name !== 'NodeLink' &&
    to.name !== 'NodeCpu' &&
    to.name !== 'NodeMemory'
  ) {
    appStore.initNodeStatistic()
  }
  if (appStore.token && to.name === 'SignIn') {
    next(from)
    return
  }
  next()
})
export default router
